﻿using AntaresShell.BaseClasses;
using Repository.MODELs;
using Repository.Sync;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace Repository.Repositories
{
    public class Repository
    {
        private Repository()
        {
        }

        public static Repository Instance
        {
            get { return Nested._instance; }
        }

        // Template vairable to store anything.
        public object Variable { get; set; }

        private class Nested
        {
            /// <summary>
            /// Instance of Repository for Singleton pattern.
            /// </summary>
            internal static readonly Repository _instance = new Repository();

            /// <summary>
            /// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit.
            /// </summary>
            static Nested()
            {
            }
        }

        async public Task<ObservableCollection<TaskModel>> GetAllTasksAsync()
        {
            return new ObservableCollection<TaskModel>();
        }

        async public Task<ObservableCollection<UserModel>> GetAllUserAsync()
        {
            var projects = await ProjectMemberRepository.Instance.GetAllProjects(GlobalData.MyUserID);
            var userList = new ObservableCollection<UserModel>();

            if (projects != null)
            {
                foreach (var project in projects)
                {
                    var users = await ProjectMemberRepository.Instance.GetAllProjectsMember(project.ProjectID);
                    if (users != null)
                    {
                        foreach (var userModel in users)
                        {
                            if (userList.FirstOrDefault(p => p.UserID == userModel.UserID) == null)
                            {
                                var info = await UserInformationRepository.Instance.GetUser(userModel.UserID);
                                userList.Add(info);
                            }
                        }
                    }

                }
            }

            return userList;
        }

        async public Task<SearchableBaseModel[]> GetSeachableBaseModelAsync()
        {
            var tasks = await TaskRepository.Instance.GetAllTasksForUser(GlobalData.MyUserID);
            var projects = await ProjectRepository.Instance.GetAllProjects();
            var users = await GetAllUserAsync();
            var result = new List<SearchableBaseModel>();

            if (tasks != null)
            {
                result.AddRange(tasks);
            }
            if (projects != null)
            {
                result.AddRange(projects);
            }
            if (users != null)
            {
                result.AddRange(users);
            }

            return result.ToArray();
        }
    }
}
